package com.example;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDAO {
    
    // 获取所有课程
    public List<Course> getAllCourses() {
        List<Course> courses = new ArrayList<>();
        String sql = "SELECT * FROM courses ORDER BY id";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Course course = new Course(
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getDouble("credits"),
                    rs.getString("department"),
                    rs.getString("description"),
                    rs.getString("courseType"),
                    rs.getString("semester")
                );
                courses.add(course);
            }
        } catch (SQLException e) {
            System.err.println("获取课程列表失败: " + e.getMessage());
        }
        return courses;
    }
    
    // 添加新课程
    public boolean addCourse(Course course) {
        String sql = "INSERT INTO courses (name, credits, department, description, courseType, semester) VALUES (?, ?, ?, ?, ?, ?)";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, course.getName());
            pstmt.setDouble(2, course.getCredits());
            pstmt.setString(3, course.getDepartment());
            pstmt.setString(4, course.getDescription());
            pstmt.setString(5, course.getCourseType());
            pstmt.setString(6, course.getSemester());
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("添加课程失败: " + e.getMessage());
            return false;
        }
    }
    
    // 更新课程信息
    public boolean updateCourse(Course course) {
        String sql = "UPDATE courses SET name=?, credits=?, department=?, description=?, courseType=?, semester=? WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, course.getName());
            pstmt.setDouble(2, course.getCredits());
            pstmt.setString(3, course.getDepartment());
            pstmt.setString(4, course.getDescription());
            pstmt.setString(5, course.getCourseType());
            pstmt.setString(6, course.getSemester());
            pstmt.setInt(7, course.getId());
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("更新课程失败: " + e.getMessage());
            return false;
        }
    }
    
    // 删除课程
    public boolean deleteCourse(int courseId) {
        String sql = "DELETE FROM courses WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, courseId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("删除课程失败: " + e.getMessage());
            return false;
        }
    }
    
    // 根据ID获取课程
    public Course getCourseById(int courseId) {
        String sql = "SELECT * FROM courses WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, courseId);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                return new Course(
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getDouble("credits"),
                    rs.getString("department"),
                    rs.getString("description"),
                    rs.getString("courseType"),
                    rs.getString("semester")
                );
            }
        } catch (SQLException e) {
            System.err.println("获取课程信息失败: " + e.getMessage());
        }
        return null;
    }
    
    // 搜索课程
    public List<Course> searchCourses(String keyword) {
        List<Course> courses = new ArrayList<>();
        String sql = "SELECT * FROM courses WHERE name LIKE ? OR department LIKE ? OR description LIKE ? ORDER BY id";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            String searchPattern = "%" + keyword + "%";
            pstmt.setString(1, searchPattern);
            pstmt.setString(2, searchPattern);
            pstmt.setString(3, searchPattern);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Course course = new Course(
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getDouble("credits"),
                    rs.getString("department"),
                    rs.getString("description"),
                    rs.getString("courseType"),
                    rs.getString("semester")
                );
                courses.add(course);
            }
        } catch (SQLException e) {
            System.err.println("搜索课程失败: " + e.getMessage());
        }
        return courses;
    }
} 